const a = 6378245.0;
const ee = 0.00669342162296594323;

export const gcj02towgs84 = ($lng, $lat) => {
    let $dlat = transformlat($lng - 105.0, $lat - 35.0);
    let $dlng = transformlng($lng - 105.0, $lat - 35.0);
    const $radlat = $lat / 180.0 * Math.PI;
    let $magic = Math.sin($radlat);
    $magic = 1 - ee * $magic * $magic;
    const $sqrtmagic = Math.sqrt($magic);
    $dlat = ($dlat * 180.0) / ((a * (1 - ee)) / ($magic * $sqrtmagic) * Math.PI);
    $dlng = ($dlng * 180.0) / (a / $sqrtmagic * Math.cos($radlat) * Math.PI);
    const $mglat = $lat + $dlat;
    const $mglng = $lng + $dlng;
    return [$lng * 2 - $mglng, $lat * 2 - $mglat];
}

const transformlat = ($lng, $lat) => {
    let $ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * Math.sqrt(Math.abs($lng));
    $ret += (20.0 * Math.sin(6.0 * $lng * Math.PI) + 20.0 * Math.sin(2.0 * $lng * Math.PI)) * 2.0 / 3.0;
    $ret += (20.0 * Math.sin($lat * Math.PI) + 40.0 * Math.sin($lat / 3.0 * Math.PI)) * 2.0 / 3.0;
    $ret += (160.0 * Math.sin($lat / 12.0 * Math.PI) + 320 * Math.sin($lat * Math.PI / 30.0)) * 2.0 / 3.0;
    return $ret;
}

const transformlng = ($lng, $lat) => {
    let $ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * Math.sqrt(Math.abs($lng));
    $ret += (20.0 * Math.sin(6.0 * $lng * Math.PI) + 20.0 * Math.sin(2.0 * $lng * Math.PI)) * 2.0 / 3.0;
    $ret += (20.0 * Math.sin($lng * Math.PI) + 40.0 * Math.sin($lng / 3.0 * Math.PI)) * 2.0 / 3.0;
    $ret += (150.0 * Math.sin($lng / 12.0 * Math.PI) + 300.0 * Math.sin($lng / 30.0 * Math.PI)) * 2.0 / 3.0;
    return $ret;
}